<template>
  <div class="app-container">
    <el-row :gutter="10" v-if="optType != 'view'" class="mb8">
      <el-col :span="1.5">
        <el-button type="primary" plain icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
      <!-- <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-if="buttonType!='2'"
          v-hasPermi="['mes:pro:proroute:remove']"
        >删除</el-button>
      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="routeproductbomList" @selection-change="handleSelectionChange" max-height="620">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="序号" align="center" width="50px">
        <template slot-scope="scope">
          <span>{{ scope.$index + 1 }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产品物料编码" align="center" prop="itemCode" />
      <el-table-column label="产品物料名称" align="center" prop="itemName" width="200px" />
      <el-table-column label="规格型号" align="center" prop="specification" width="200px" />
      <el-table-column label="用量:分子" align="center" prop="jdyNumerator">
        <template slot-scope="scope">
          <el-input v-model="scope.row.jdyNumerator" @change="(e) => changeInput(e, scope.$index, scope.row)" placeholder="请输入分子" type="number" :disabled="buttonType == '2'"></el-input>
        </template>
      </el-table-column>
      <!-- add begin by zyj 2024/10/17 生产工单-工艺维护 标注物料是否加工 -->
      <el-table-column label="是否加工" align="center" prop="process">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.process" :active-value="1" :inactive-value="0" @change="(e) => changeInput(e, scope.$index, scope.row)" :disabled="buttonType == '2'"></el-switch>
        </template>
      </el-table-column>
      <!-- add end by zyj 2024/10/17 生产工单-工艺维护 标注物料是否加工 -->
      <el-table-column label="用量:分母" width="90px" align="center" prop="jdyDenominator">
        <!-- <template slot-scope="scope">
          <span>{{
            scope.row && scope.row.jdyDenominator ? scope.row.jdyDenominator : 1
          }}</span>
        </template>-->
      </el-table-column>
      <el-table-column label="单位" align="center" prop="unitOfMeasure" />
      <!--      <el-table-column label="用料比例" align="center" prop="quantity" />-->
      <el-table-column label="操作" align="center" v-if="optType != 'view'" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row, scope.$index)" v-if="buttonType != '2'">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <!-- v-if="buttonType == '2'"  -->
    <pagination v-show="total > 0" :total="total" :page-sizes="[ 50, 100, 150,200]" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />
    <ItemBomSelectSingle ref="itemBom1" v-if="openSelect" :itemId="productId" :routeId="routeId" :processId="processId" :processName="processName" :processCode="processCode" @backEndData="getList"></ItemBomSelectSingle>
  </div>
</template>

<script>
import {
  listRouteproductbom,
  getRouteproductbom,
  delRouteproductbom,
  addRouteproductbom,
  updateRouteproductbom
} from "@/api/mes/pro/routeproductbom";
import { getProRouteItemlist, getProRouteItemlistNoPage } from "@/api/mes/pro/workorder";
import ItemBomSelectSingle from "./itemSelect.vue";
export default {
  name: "Routeproductbom",
  components: { ItemBomSelectSingle },
  props: {
    routeId: null,
    productId: null,
    processId: null,
    processName: null,
    processCode: null,
    optType: null,
    buttonType: null,
    subassemblyList: null,
    carftForm: null,
    workorderId: null,
    bomItemCode: null
  },
  data() {
    return {
      // 遮罩层&
      loading: false,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      openSelect: false,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 产品制程物料BOM表格数据
      routeproductbomList: [],
      allList: this.subassemblyList,
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        // jdyItemId: this.jdyItemId,
        pageNum: 1,
        pageSize: 50,
        routeId: this.carftForm.routeId,
        processId: this.processId,
        productId: this.carftForm.productId,
        workorderId: this.carftForm.workorderId,
        bomItemCode: this.carftForm.bomItemCode,
        workorderCode: this.carftForm.workorderCode
        // itemId: this.productId,
        // itemCode: null,
        // itemName: null,
        // specification: null,
        // unitOfMeasure: null,
        // quantity: null,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        routeId: [
          { required: true, message: "工艺路线不能为空", trigger: "blur" }
        ],
        processId: [
          { required: true, message: "工序不能为空", trigger: "blur" }
        ],
        productId: [
          { required: true, message: "产品物料不能为空", trigger: "blur" }
        ],
        itemId: [
          { required: true, message: "产品BOM物料不能为空", trigger: "blur" }
        ]
      }
    };
  },
  created() {
    // this.init();
  },
  watch: {
    productId: function (val) {
      this.productId = val;
    },
    productCode: function (val) {
      this.productCode = val;
    },
    productName: function (val) {
      this.productName = val;
    },
    // workorderId: function (val) {
    //   this.workorderId = val;
    //   // this.getList();
    // },
    carftForm: function (val) {
      this.workorderId = val;
      // this.getList();
    },
    subassemblyList: function (val) {
      this.allList = val;
    },
    workorderId: function (val) {
      this.queryParams.workorderId = val;
    },
    productId: {
      handler(val) {
        this.queryParams.productId = val;
      },
    },
    processId: {
      handler(val) {
        this.queryParams.processId = val;
      },
    },
    routeId: {
      handler(val) {
        this.queryParams.routeId = val;
      },
    },
    bomItemCode: {
      handler(val) {
        this.queryParams.bomItemCode = val;
      },
    },
  },
  methods: {
    init() {
      console.log('工艺维护？我被触发了')
      delete this.queryParams.productId
      this.queryParams.pageNum = 1
      this.getList()
    },
    /** 查询产品制程物料BOM列表 */
    getList() {
      // if (msg != undefined) {
      //   let Repetitive = this.compare(msg, this.routeproductbomList);
      //   console.log(Repetitive, "Repetitive");
      //   if (Repetitive.length != 0) {
      //     this.$modal.msgError("产品物料编码为" + Repetitive + "已被选择");
      //     return;
      //   }
      //   this.routeproductbomList = this.routeproductbomList.concat(msg);
      //   console.log(msg, "子组件传过来 的数据");
      //   this.$emit("backEndData", msg, "add");
      // }
      this.loading = true;
      getProRouteItemlist(this.queryParams).then(response => {
        console.info("response.rows", response.rows);
        this.routeproductbomList = response.rows;
        this.total = response.total
        this.$emit("backEndData", response.rows, "add");
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      });
    },
    compare(arr1, arr2) {
      return arr1.filter(v => {
        return arr2.includes(v);
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        jdyItemId: null,
        recordId: null,
        routeId: this.routeId,
        processId: this.processId,
        workorderCode: this.carftForm.workorderCode,
        productId: this.productId,
        itemId: null,
        itemCode: null,
        itemName: null,
        specification: null,
        unitOfMeasure: null,
        quantity: null,
        remark: null,
        attr1: null,
        attr2: null,
        attr3: null,
        attr4: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.recordId);
      this.single = selection.length !== 1;
      this.multiple = !selection.length;
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.openSelect = true;
      let Param = {
        "routeId": this.routeId,
        "workorderId": this.workorderId,
        "bomItemCode": this.bomItemCode,
        "workorderCode": this.carftForm.workorderCode
      }
      getProRouteItemlistNoPage(Param).then(msg => {
        let obj = {
          processId: this.processId,
          processCode: this.processCode,
          processName: this.processName,
          workorderCode: this.carftForm.workorderCode
        };
        this.$nextTick(() => {
          this.$refs.itemBom1.init(
            // this.allList,
            msg.data,
            this.routeproductbomList,
            this.carftForm,
            obj
          );
          this.$refs.itemBom1.showFlag = true;
        });
      })



      this.title = "添加产品制程物料BOM";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const recordId = row.recordId || this.ids;
      getRouteproductbom(recordId).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改产品制程物料BOM";
      });
    },
    /** 提交按钮 */
    // submitForm() {
    //   this.$refs["form"].validate(valid => {
    //     if (valid) {
    //       if (this.form.recordId != null) {
    //         updateRouteproductbom(this.form).then(response => {
    //           this.$modal.msgSuccess("修改成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       } else {
    //         addRouteproductbom(this.form).then(response => {
    //           this.$modal.msgSuccess("新增成功");
    //           this.open = false;
    //           this.getList();
    //         });
    //       }
    //     }
    //   });
    // },
    /** 删除按钮操作 */
    handleDelete(row, index) {
      // console.log(row, index);
      // this.routeproductbomList.splice(index, 1);
      // console.log(
      //   this.routeproductbomList,
      //   "=======this.routeproductbomList====="
      // );
      const recordIds = row.recordId || this.ids;
      this.$modal
        .confirm(
          '是否确认删除产品制程物料BOM编号为"' + recordIds + '"的数据项？'
        )
        .then(function () {
          return delRouteproductbom(recordIds);
        })
        .then(() => {
          this.getList();
          this.$modal.msgSuccess("删除成功");
        })
        .catch(() => { });

      // let ids = JSON.parse(localStorage.getItem("Ids"));
      // ids = ids.filter(function(item) {
      //   return item !== row.itemCode;
      // });
      // console.log(ids, "=======ids======");
      // localStorage.setItem("Ids", JSON.stringify(ids));
      this.$emit("backEndData", row, "del");
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download(
        "pro/routeproductbom/export",
        {
          ...this.queryParams
        },
        `routeproductbom_${new Date().getTime()}.xlsx`
      );
    },
    handleItemBomSelect() {
      this.$refs.itemBom.showFlag = true;
      this.$refs.itemBom.getList();
    },
    // onItemBomSelected(row){
    //   this.form.itemId = row.itemId;
    //   this.form.itemCode = row.itemCode;
    //   this.form.itemName = row.sonname;
    //   this.form.specification = row.specification;
    //   this.form.measureName = row.measureName;
    //   this.form.unitOfMeasure = row.unitOfMeasure;
    //   this.form.quantity = row.quantity;
    // }
    // onItemBomSelected(backEndData){
    //   console.info('backEndData====>',backEndData)
    // }
    //输入分子的change
    changeInput(e, index, row) {
      console.log(e, index, row, "============indexe");
      let query = { ...row }
      updateRouteproductbom(query).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.open = false;
        this.getList();
      });
    }
  }
};
</script>
